package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //select * from pms_attr_group t where t.catelog_id=? and (t.attr_group_name like '%key%' or t.descript like '%key%' or t.attr_group_id like '%key%')
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            wrapper.and((queryWrapper) -> {
                queryWrapper.like("attr_group_name", key)//属性组名称
                        .or()
                        .like("descript", key);//描述
                //如果是数字格式 ^\d{n,}$
                boolean isMatch = Pattern.matches("^\\d{1,}$", key);
                if (isMatch) {
                    Long attrGroupId = Long.parseLong(key);
                    queryWrapper.or()
                            .eq("attr_group_id", attrGroupId);//属性组id
                }
            });
        }
        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }

        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    /**
     * 根据分类id查出所有分组以及这些组里面的属性
     *
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVO> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        List<AttrGroupWithAttrVO> result = new ArrayList<>();
        //1、查出当前分类下的所有属性分组
        List<AttrGroupEntity> attrGroupEntityList = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2、查出每个属性分组下的所有属性
        if (CollectionUtils.isNotEmpty(attrGroupEntityList)) {
            for (AttrGroupEntity attrGroupEntity : attrGroupEntityList) {
                AttrGroupWithAttrVO attrGroupWithAttrVO = new AttrGroupWithAttrVO();
                BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrVO);
                result.add(attrGroupWithAttrVO);
            }

            result.forEach(attrGroupWithAttrVO -> {
                List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(
                        new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupWithAttrVO.getAttrGroupId()));
                List<AttrEntity> attrEntityList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(relationEntities)) {
                    List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
                    attrEntityList = attrDao.selectBatchIds(attrIds);
                }
                attrGroupWithAttrVO.setAttrs(attrEntityList);
            });
        }
        return result;
    }

}